package lxf.qs.com;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;

import javax.swing.JFrame;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

public class FtpTool { 
	ShowWait showWait ;
    String userName ;  
    String userPassword;  
    String server;//直接ip地址  
    String path;  
    public FTPClient ftpClient;
    public FtpTool(String server, String path,String userName,String userPassword) {
		super();
		this.userName = userName;
		this.userPassword = userPassword;
		this.server = server;
		this.path = path;
		showWait = new ShowWait();
	}

	public static void main(String[] args) {
    	FtpTool ftpTset = new FtpTool("sk595.webcname.net","Web/QS2015/","ftp431442","LXFlxf158");
    	long t1 = System.currentTimeMillis()/1000;
    	ftpTset.connectFtp();
    	ftpTset.uploadFile("ftp9.txt","hello world，你dfds s gsd g好世界");
    	long t2 = System.currentTimeMillis()/1000;
    	System.out.println("1---"+(t2-t1));
    	ftpTset.downFile("D:\\ftpTest\\","ftp9.txt");
    	t1 = System.currentTimeMillis()/1000;
    	System.out.println("2---"+(t1-t2));
    	ftpTset.close();
	}
  
    public void uploadFile(String fileName,String fileContent) {  
        try {  
        	showWait.show("正在上传文件");
            InputStream is = null;  
            // 1.输入流  
            is = new ByteArrayInputStream(fileContent.getBytes());  
            // 4.指定写入的目录  
            ftpClient.changeWorkingDirectory(path);  
            // 5.写操作  
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);  
            ftpClient.storeFile(new String(fileName.getBytes("utf-8"),"iso-8859-1"), is);  
            is.close();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }
        showWait.release();
    }  
      
    /** 
     * ftp下载数据 
     */  
   
    public void downFile(String localPath,String fileName) {  
        try {  
        	showWait.show("正在下载文件");
            int reply;  
            //3.判断登陆是否成功  
            reply = ftpClient.getReplyCode();  
            if (!FTPReply.isPositiveCompletion(reply)) {  
            	ftpClient.disconnect();  
            }  
            //4.指定要下载的目录  
            ftpClient.changeWorkingDirectory(path);// 转移到FTP服务器目录  
            //5.遍历下载的目录  
            FTPFile[] fs = ftpClient.listFiles();  
            for (FTPFile ff : fs) {  
                //解决中文乱码问题，两次解码  
                byte[] bytes=ff.getName().getBytes("iso-8859-1");  
                String fn=new String(bytes,"utf8");  
                if (fn.equals(fileName)) {  
                    //6.写操作，将其写入到本地文件中  
                    File localFile = new File(localPath + ff.getName());  
                    OutputStream is = new FileOutputStream(localFile);  
                    ftpClient.retrieveFile(ff.getName(), is);  
                    is.close();  
                }  
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        }
        showWait.release();
    } 
    public void connectFtp(){
    	 ftpClient = new FTPClient();  
         try {
        	//1.连接服务器  
        	 showWait.show("正在连接服务器");
			ftpClient.connect(server);
			   //2.登录服务器 如果采用默认端口，可以使用ftp.connect(url)的方式直接连接FTP服务器  
			//ftpClient.enterLocalPassiveMode();   //设置ftp访问模式为被动访问。能解决WiFi软件屏蔽端口、多层局域网报错的问题。
			ftpClient.setDataTimeout(60000);
	        ftpClient.login(userName, userPassword);  
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
         showWait.release();
    }
    public void close(){
    	 try {
    		 showWait.show("正在关闭连接");
			ftpClient.logout();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
        if (ftpClient.isConnected()) {  
            try {  
            	ftpClient.disconnect();  
            } catch (IOException ioe) { 
            	ioe.printStackTrace();
            }  
        }
        showWait.release();
    }
 }  


class ShowWait{   
	JFrame frame;
	public ShowWait() {
		
	}
	public void show(String title) {
		frame = new JFrame();
		frame.setBounds(500, 300, 320, 260);
		frame.setTitle(title);
		frame.setVisible(true);
	}
	public void release(){
		frame.setVisible(false);
		frame=null;
	}
}
